package com.pj.xfxh.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pj.project.sys_client.SysClientMapper;
import com.pj.project4sp.config.SpConfigMapper;
import com.pj.utils.sg.AjaxJson;
import com.pj.xfxh.project.entity.Topic;
import com.pj.xfxh.project.entity.TopicMessage;
import com.pj.xfxh.project.mapper.TopicMapper;
import com.pj.xfxh.project.service.TopicMessageService;
import com.pj.xfxh.project.service.TopicService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;

@RestController
@RequestMapping("/topic")
public class TopicController {
    /** 底层 Mapper 对象 */
    @Autowired
    SpConfigMapper spConfigMapper;
    @Autowired
    private TopicMapper topicMapper;
    @Autowired
    private TopicService topicService;
    @Autowired
    private TopicMessageService topicMessageService;

    @GetMapping("/{topicId}")
    public ResponseEntity<Topic> getTopicById(@PathVariable int topicId) {
        Topic topic = topicMapper.selectById(topicId);
        return ResponseEntity.ok(topic);
    }

    /**
     * 分页查询
     * @param topic
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<Page<Topic>> page(@RequestBody Topic topic,
                                                    @RequestParam(defaultValue = "1") Long current,
                                                    @RequestParam(defaultValue = "10") Long size) {
        QueryWrapper<Topic> queryWrapper = Wrappers.query();

        queryWrapper.lambda()
                .eq(topic.getUserId() != null, Topic::getUserId, topic.getUserId())
                .eq(topic.getTopicId() != null, Topic::getTopicId, topic.getTopicId())
                .eq(topic.getPlatformId()!=null, Topic::getPlatformId, topic.getPlatformId())
                .like(StringUtils.isNotEmpty(topic.getTopicName()), Topic::getTopicName, topic.getTopicName());

        Page<Topic> page = new Page<>(current, size);
        Page<Topic> topicPage = topicService.page(page, queryWrapper);

        return ResponseEntity.ok(topicPage);

    }

    @GetMapping("/list")
    public ResponseEntity<List<Topic>> getAllTopics(@RequestBody Topic topic) {

        QueryWrapper<Topic> queryWrapper = Wrappers.query();

        queryWrapper.lambda()
                .eq(topic.getUserId()!=null, Topic::getUserId, topic.getUserId())
                .eq(topic.getTopicId()!=null, Topic::getTopicId, topic.getTopicId())
                .eq(topic.getPlatformId()!=null, Topic::getPlatformId, topic.getPlatformId())
                .like(StringUtils.isNotEmpty(topic.getTopicName()), Topic::getTopicName, topic.getTopicName());

        List<Topic> topicList = topicService.list(queryWrapper);

        return ResponseEntity.ok(topicList);
    }

    @PostMapping("/create")
    public AjaxJson createTopic(@RequestBody Topic topic) {

        QueryWrapper<Topic> queryWrapper = Wrappers.query();
        queryWrapper.lambda()
                .eq(Topic::getUserId, topic.getUserId())
                .eq(Topic::getPlatformId, topic.getPlatformId());
        long count = topicService.count(queryWrapper);

        if(count>=10){
            return AjaxJson.getError("您已超过十个话题");
        }

        topicMapper.insert(topic);
        return AjaxJson.getSuccess();
    }

    @PutMapping("/update")
    public ResponseEntity<String> updateTopic(@RequestBody Topic topic) {
        topicMapper.updateById(topic);
        return ResponseEntity.ok("Topic updated successfully");
    }

    @DeleteMapping("/delete/{topicId}")
    public AjaxJson deleteTopic(@PathVariable int topicId) throws Exception {
        try{
            topicMapper.deleteById(topicId);
            QueryWrapper<TopicMessage> queryWrapper = Wrappers.query();
            queryWrapper.eq("topic_id",topicId);
            topicMessageService.remove(queryWrapper);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxJson.getError("删除失败！");
        }
        return AjaxJson.getSuccess("删除成功！");
    }
}
